
alter  procedure p_accmonthrange
  @curryear integer,
  @currmonth integer,
  @firstday datetime output,
  @lastday datetime output

as  

begin
  set nocount on
             
  declare   @icount integer
  declare   @fday  datetime
  declare   @lday  datetime
  declare   @cyear  varchar(4)
  declare   @cmonth varchar(2)
  select @icount=count(*) from tfd_monthlycalendar where yearid=@curryear and monthid=@currmonth
  select @fday=bdate,@lday=edate from tfd_monthlycalendar where yearid=@curryear and monthid=@currmonth
  if @icount>0
  begin
   set @firstday = @fday
   set @lastday =  cast(CONVERT(varchar(10), @lday, 25)+' 23:59:59.997' as datetime)
  end
  else
  begin
   set @cyear = cast(@curryear as char(4))
   set @cmonth = cast(@currmonth as char(2))
   set @firstday = dbo.f_castdate(@cmonth, 1, @cyear)--cast(@cmonth+'/01/'+@cyear as datetime);
   if(@currmonth=12)
   begin
     set @curryear = @curryear+1
     set @currmonth = 1
   end
   else
   begin
     set @curryear = @curryear
     set @currmonth = @currmonth+1
   end
   set @cyear = cast(@curryear as char(4))
   set @cmonth = cast(@currmonth as char(2))
   set @lastday = dbo.f_castdate(@cmonth, 1, @cyear)-0.00000005--2018-01-30 23:59:59.997
  end
end

GO
